<template>
  <div class="bgc">
    <el-row :gutter="20" style="margin-top: 20px">
      <el-col :span="10">
        <div class="grid-content bg-purple mag" >
          <div style="height: 40px;margin-top: 20px" >
            <span style="padding:20px 0 0 20px;display: block">{{ fromTitle }}</span>
          </div>
          <el-divider></el-divider>
          <div style="padding: 0 20px 10px 10px">
            <el-form ref="form" :rules="rules" :model="form" label-width="80px">
              <el-form-item label="标签名称" prop="labelName">
                <el-input v-model="form.labelName"></el-input>
              </el-form-item>
              <el-form-item label="所属分类" prop="sortId">
                <el-select
                  v-model="form.sortName"
                  @change="change">
                  <el-option
                    v-for="item in sortList"
                    :key="item.sortId"
                    :label="item.sortName"
                    :value="item.sortId">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmit('form')">立即添加</el-button>
                <el-button type="primary" @click="updateSubmit">立即修改</el-button>
                <el-button @click="resetForm('form')">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </el-col>
      <el-col :span="14">
        <div class="grid-content bg-purple mag">
          <div style="height: 40px;margin-top: 20px" >
            <span style="padding:20px 0 0 20px;display: block">{{ categoryList }}</span>
          </div>
          <el-divider></el-divider>
          <div style="padding: 0 20px 10px 10px">
            <el-table
              v-loading="listLoading"
              :data="tableData"
              :current-page="pageNum"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="100"
              layout="total, sizes, prev, pager, next, jumper"
              element-loading-text="Loading"
              fit
              highlight-current-row
              stripe
            >
              <el-table-column
                prop="labelId"
                label="id"
                width="100">
              </el-table-column>
              <el-table-column
                prop="labelName"
                label="标签名"
                width="100">
              </el-table-column>
              <el-table-column
                prop="sortName"
                label="所属分类">
              </el-table-column>
              <el-table-column
                align="right">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    plain
                    @click="handleEdit(scope.row)">修改</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    plain
                    @click="handleDelete(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="block">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page.sync="currentPage1"
                :page-size="pageSize"
                layout="total, prev, pager, next"
                :total="total">
              </el-pagination>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { addLabel, updateLabel, delLabel } from '@/api/label'
import { getLabelList } from '@/api/table'
import { getArticleListToIsShow } from '@/api/sort'

export default {
  name: 'LabelNavigation',
  data() {
    return {
      // 表单校验
      rules: {
        labelName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ],
        sortId: [
          { required: true, message: '请选择分类', trigger: 'change' }
        ]
      },
      tableData: [],
      fromTitle: '修改标题',
      categoryList: '分类列表',
      pageNum: 1,
      total: 0,
      pageSize: 8,
      listLoading: false,
      page: 1,
      form: {
        labelId: 0,
        labelName: '',
        sortId: '',
        sortName: ''
      },
      updateForm: {
        labelId: 0,
        labelName: '',
        sortId: '',
        sortName: ''
      },
      sortList: [],
      sortId: 1
    }
  },
  created() {
    this.findLabel()
    this.findSortList()
  },
  methods: {
    change(val) {
      this.form.sortId = val
      console.log(val)
    },
    currentPage1() {},
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.findLabel()
    },
    findSortList() {
      getArticleListToIsShow().then(res => {
        this.sortList = res.data
        console.log(this.sortList)
      })
    },
    findLabel() {
      getLabelList({ pageNum: this.pageNum, pageSize: this.pageSize }).then(response => {
        this.total = response.total
        this.tableData = response.data
        console.log(this.total)
        this.listLoading = false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 添加分类
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.form)
          addLabel(this.form).then(response => {
            this.findLabel()
            this.$message.success('添加成功')
          })
        } else {
          return false
        }
      })
    },
    updateSubmit() {
      this.$confirm('是否修改?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateLabel(this.form).then(response => {
          this.$message.success('修改成功')
          this.findLabel()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        })
      })
    },
    handleEdit(index, row) {
      this.updateForm = index
      console.log(this.updateForm)
      this.from = this.updateForm
      console.log(this.from)
      this.form = index
    },
    handleDelete(index, row) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delLabel({ labelId: index.labelId }).then(res => {
          this.$message.success('删除成功')
          this.findLabel()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
.bgc {
  background: #EBEEF5;
  height: 100%;
  margin: 20px 0 0 0;
  padding: 0 10px 0 10px;
}
.mag {
  margin: 0 5px 0 5px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  background-color: #ffffff;
}
</style>
